<?php

namespace App\Http\Controllers\View;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class AuthController extends Controller
{


    public function __construct()
    {
        $this->middleware('guest')->only('loginWeb');
    }

    public function loginWeb()
    {
        return view('auth.login');
    }



    public function loginOpt(Request $request)
    {
        $data = $this->validate($request, [
            "email" => ['required','email'],
            "password" => ['required','min:6']
        ]);


        if (Auth::attempt($data,$request->has('remember'))) {
            if (Auth::user()->is_active) {
                session()->flash('success',"登录成功,欢迎回来");
                $default_url = route('users.show',[Auth::user()]);
                return redirect()->intended($default_url);
            } else {
                Auth::logout();
                session()->flash('warning',"用户未激活");
                return redirect()->route('login')->withInput();
            }

        } else {
            session()->flash('danger','登录失败，账号和密码不相匹配');
            return redirect()->back()->withInput();
        }
    }



    public function logout()
    {
        Auth::logout();
        session()->flash('success',"账号退出成功");
        return redirect()->route('login');
    }
}
